Master = {}

local global = require "global"
local ServerResMgr = require "server_res_mgr"
local MapMgr = require "map.map_mgr"
local NetForServer = require "network.net_for_server"
local MapServer = require "network.map_server"
local ClientSessionMgr = require "client_session.client_session_mgr"

local protoc = require "protoc"
local serpent = require "serpent"

local util = require("util")

function Master.fun_stop_callback(timer_id)
	----------------------------------------------
	global.net_for_server:Stop()
	
	----------------------------------------------
	crossover.remove_timer(timer_id)
	crossover.exit()
end

function Master.fun_console_callback(cmd)
	if cmd == "exit" then
		global.master:Stop()
	elseif cmd == "r" then
		--util.HotUpdateCode()
		--util.HotUpdateTable()
		util.HotUpdateAllCode()
		util.HotUpdateAllTable()
	elseif cmd == "show server" then
		global.net_for_server:ShowServer()
		LOG_INFO("-------------------------------------------------------------------------------------------")
		global.map_server:ShowServer()
	end
end

function Master.GameLoop(timer_id)
    
end

function Master:New(o)
    o = o or {}	
	self.__index = self
	setmetatable(o, self)
	
	o.stopping_ = false
    return o
end

function Master:Run()
	crossover.set_console_handler(Master.fun_console_callback)

	global.server_res_mgr = ServerResMgr:New()
	global.server_res_mgr:LoadRes()
	
	global.map_server = MapServer:New()
	global.map_server:Init()
	global.map_server:Start()
	
	global.net_for_server = NetForServer:New()
	global.net_for_server:Init()

	global.map_mgr = MapMgr:New()
	global.client_session_mgr = ClientSessionMgr:New()

	LOG_INFO("Init(): mapserver started")
	crossover.save_log()

end

function Master:Stop()
	crossover.stop(Master.fun_stop_callback)
end

function Master:is_stopping()
	return stopping_
end

function Master:StartGameLoop()
    crossover.add_timer(200, Master.GameLoop)
end


return Master